package com.dayaokeji.rhythmschoolstudent.client.mine.group;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.event.GroupEvent;
import com.dayaokeji.rhythmschoolstudent.event.UpdateGroupEvent;
import com.dayaokeji.rhythmschoolstudent.utils.DialogUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.wiget.CreateGroupDialog;
import com.dayaokeji.rhythmschoolstudent.wiget.GroupInfo;
import com.dayaokeji.rhythmschoolstudent.wiget.SingleInputDialog;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.GroupApi;
import com.dayaokeji.server_api.domain.JoinGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 群组
 */
public class GroupActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private static final GroupApi groupApi = ApiUtils.getApi(GroupApi.class);
    private Call<ServerResponse<Void>> addCall;

    public static void navigation(Context context) {
        Intent intent = new Intent(context, GroupActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        setSupportActionBar(toolbar);
        EventBus.getDefault().register(this);
        setFragment();
    }

    private void setFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.content,GroupListFragment.newInstance()).commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.group,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_group:
                addGroupDialog();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void existAndDelGroupEvent(GroupEvent groupEvent) {
        ActivityStackManager.finish(this);
    }


    /**
     * 加入群组对话框
     */
    private void addGroupDialog() {
        final SingleInputDialog singleInputDialog = SingleInputDialog.newInstance("加入群组", "请输入群ID");
        singleInputDialog.setInputType(SingleInputDialog.InType.NUMBER);
        singleInputDialog.show(getSupportFragmentManager(), "add_group");
        singleInputDialog.setOnClickListener(new SingleInputDialog.OnClickListener() {
            @Override
            public void onClick(String str) {
                if (!TextUtils.isEmpty(str)) {
                    addGroup(str);
                    singleInputDialog.dismiss();
                }else{
                    ToastUtils.info("请输入群组ID");
                }
            }
        });
    }

    /**
     * 加入群组
     * @param groupId
     */
    private void addGroup(String groupId) {
        JoinGroup addGroup = new JoinGroup();
        addGroup.setGroupId(groupId);

        addCall = groupApi.add(addGroup);
        addCall.enqueue(new ServerCallback<Void>(this,"正在加入...") {
            @Override
            public void onResponse(boolean success, ServerResponse<Void> response) {
                if (success) {
                    ToastUtils.success("加入群组成功");
                    EventBus.getDefault().post(new UpdateGroupEvent());
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (addCall != null) {
            addCall.cancel();
        }
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
